package com.yugao.fintech.draper.admin.controller;

import cn.hutool.core.lang.Dict;
import com.yugao.fintech.draper.admin.service.FileService;
import com.yugao.fintech.draper.admin.param.FileInfoVO;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.log.annotation.AuditLog;
import com.yugao.fintech.draper.log.enums.BusinessType;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.admin.param.FileDownloadDTO;
import com.yugao.fintech.draper.admin.param.FileQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
@Tag(name = "文件管理")
public class FileController {
    private final FileService fileService;

    @Operation(summary = "查询文件列表分页")
    @GetMapping(value = "/page")
    public ResultVO<PageResult<FileInfoVO>> pageQuery(@RequestBody FileQuery fileQuery) {
        return ResultVO.success(PageResult.success(fileService.pageQuery(fileQuery)));
    }

    @Operation(summary = "修改文件")
    @PostMapping(value = "/update")
    public ResultVO<Boolean> update(@RequestBody FileInfoVO fileInfoVO) {
        return ResultVO.success(fileService.updateFile(fileInfoVO));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping(value = "/delete")
    public ResultVO<Boolean> delete(Long id) {
        return ResultVO.success(fileService.deleteFile(id));
    }

    @Operation(summary = "获取文件详细信息")
    @GetMapping(value = "/findById")
    public ResultVO<FileInfoVO> findById(Long id) {
        return ResultVO.success(fileService.findById(id));
    }

    @Operation(summary = "获取全部文件")
    @GetMapping(value = "/findAll")
    public ResultVO<List<FileInfoVO>> findAll() {
        return ResultVO.success(fileService.findAll());
    }

    @AuditLog(value = "文件管理", businessType = BusinessType.INSERT)
    @Operation(summary = "上传文件")
    @PostMapping(value = "/add")
    public ResultVO<Dict> add(@RequestPart("oss") MultipartFile file) {
        return ResultVO.success(fileService.uploadFile(file));
    }

    @Operation(summary = "下载文件")
    @PostMapping("/download")
    public void download(@RequestBody FileDownloadDTO fileDownloadDTO, HttpServletResponse response) {
        fileService.getFile(fileDownloadDTO.getBucket(), fileDownloadDTO.getFileName(), response);
    }
}
